package cn.chenxinjie.hrm.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 陈鑫杰
 * @date 2022/3/22 14:03
 */
@Component
public class HrmBaseFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate){
        // 获取到请求中的token
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        // 获取request对象
        HttpServletRequest request = requestAttributes.getRequest();
        // 获取请求中的参数Authorization(必须是这个名字)
        String authorization = request.getHeader("Authorization");
        System.err.println(authorization);
        // 将token保存到feign调用服务发起的http请求的请求头中
        requestTemplate.header("Authorization",authorization);
    }
}
